#include "init.h"
#include "boot_info.h"
#include "cpu.h"
#include "irq.h"
#include "log.h"
#include "os_cfg.h"
#include "klib.h"
#include "task.h"
#include "cpu_instr.h"
#include "list.h"
#include "sem.h"
#include "memory.h"
#include "time.h"
#include "console.h"
#include "kbd.h"
#include "fs.h"

void kernel_init(boot_info_t* boot_info)
{
    ASSERT(boot_info->ram_region_count != 0);
    cpu_init();
    irq_init();
    log_init();
    memory_init(boot_info);
    fs_init();
    time_init();
    task_manager_init();
}

static task_t first_task;
static task_t init_task;
static uint32_t init_task_stack[1024];
static sem_t sem;

void move_to_first_task(void)
{
    task_t* cur = task_current();
    ASSERT(cur != 0);
    tss_t* tss = &(cur->tss);
    __asm__ __volatile__(
        "push %[ss]\n\t"
        "push %[esp]\n\t"
        "push %[eflags]\n\t"
        "push %[cs]\n\t"
        "push %[eip]\n\t"
        "iret"::[ss]"r"(tss->ss), [esp]"r"(tss->esp), \
        [eflags]"r"(tss->eflags), [cs]"r"(tss->cs), [eip]"r"(tss->eip)
    );
}

void init_main(void)
{
    log_printf("kernel is running...");
    log_printf("Version:%s", OS_VERSION);
    log_printf("%d %d %x %c", 123454, -123, 0x12345, 'a');

    task_first_init();
    move_to_first_task();
}
